
Public Class WebForm5
    Inherits System.Web.UI.Page
    Protected WithEvents Lbl_date As System.Web.UI.WebControls.Label
    Protected WithEvents Label6 As System.Web.UI.WebControls.Label
    Protected WithEvents lbl_user As System.Web.UI.WebControls.Label
    Protected WithEvents Label2 As System.Web.UI.WebControls.Label
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents Lblok As System.Web.UI.WebControls.Label
    Protected WithEvents btn_save As System.Web.UI.WebControls.Button
    Protected WithEvents txt_dealercode As System.Web.UI.WebControls.TextBox
    Protected WithEvents ddl_corp As System.Web.UI.WebControls.DropDownList
    Protected WithEvents BTN_SHOW As System.Web.UI.WebControls.Button
    Protected WithEvents DG_CUST As System.Web.UI.WebControls.DataGrid
    Protected WithEvents HyperLink2 As System.Web.UI.WebControls.HyperLink
    Protected WithEvents Image1 As System.Web.UI.WebControls.Image
    Protected WithEvents Image2 As System.Web.UI.WebControls.Image
    Protected WithEvents Label3 As System.Web.UI.WebControls.Label


#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        Dim SQL As String
        Dim usr As String
        Dim ds As New myDBConn()
        Session.LCID = 1055

        If Page.IsPostBack Then Exit Sub
        Me.Lbl_date.Text = Now.ToShortDateString
        usr = Request.ServerVariables("LOGON_USER")
        SQL = "SELECT a.name FROM USERS a,perms p WHERE  a.usrid=p.usrid and a.LOGUSR= '" & usr & "' and appid=5;"

        If ds.myOleDbTable(SQL).Rows.Count > 0 Then
            Me.lbl_user.Text = ds.myOleDbTable(SQL).Rows(0)(0)
        Else
            Response.Redirect("logout.htm")
        End If

        SQL = " select cu.customer_id CUSTID ,cu.custcode||' '||cc.cclname  CORP from  customer_all cu,ccontact_all cc"
        SQL = SQL & " where cc.customer_id=cu.customer_id"
        SQL = SQL & " and cc.ccbill='X'"
        SQL = SQL & " and cu.custcode like '45%'"
        Me.ddl_corp.DataSource = ds.myORATable(SQL)
        Me.ddl_corp.DataTextField = "CORP"
        Me.ddl_corp.DataValueField = "CUSTID"
        Me.ddl_corp.DataBind()

        ds = Nothing

    End Sub


    Private Sub btn_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_save.Click
        Dim ds As New myDBConn()
        Dim sql As String
        Me.Lblok.Text = ""
        Dim os_user As String = Request.ServerVariables("LOGON_USER")
        sql = "SELECT * FROM ittemp.corp_dealers where corp_dealer_id=" & Me.ddl_corp.SelectedItem.Value & ""
        If Trim(Me.txt_dealercode.Text) = "" Then
            Response.Write("<SCRIPT language=""JavaScript"">" & vbCrLf & "{" & vbCrLf)
            Response.Write("alert("" Please enter dealer code "")" & vbCrLf)
            Response.Write("}" & vbCrLf & "</SCRIPT>" & vbCrLf)
        ElseIf ds.myORATable(sql).Rows.Count > 0 Then
            Me.Lblok.Text = "Error:This corporate has  already been  inserted before. Record has not been saved!!"
        Else
            Dim LSTR As Integer = Len(Trim(Me.txt_dealercode.Text))
            Dim dcode As String = Right(Trim(Me.txt_dealercode.Text), LSTR - 1)
            sql = "select * from dealer where custcode='" & dcode & "'"
            If ds.myORATable(sql).Rows.Count > 0 Then
                sql = "insert into ittemp.corp_dealers  values (" & Me.ddl_corp.SelectedItem.Value & ",'" & Trim(Me.txt_dealercode.Text) & "','" & os_user & "',sysdate)"
                ds.ORAExecute(sql)
                Me.Lblok.Text = "Record has been saved!"
                Me.txt_dealercode.Text = ""
                If tmp = 1 Then
                    SHOW_DEALERS()
                End If
            Else
                Me.Lblok.Text = "Error:Not able to find the dealer code. Please enter valid dealer code!!"
            End If
            End If

        ds = Nothing
    End Sub

    Private Sub BTN_SHOW_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_SHOW.Click
        If tmp = 0 Then
            tmp = 1
            SHOW_DEALERS()
            Me.BTN_SHOW.Text = "Hide Corp.Dealers"

        Else
            Me.BTN_SHOW.Text = "Show Corp.Dealers"
            tmp = 0
            Me.DG_CUST.Visible = False
            Me.Lblok.Text = ""
        End If

    End Sub

    Sub SHOW_DEALERS()

        Dim SQL As String
        Dim DS As New myDBConn()

        SQL = " SELECT cus.custcode||' '||cc.CCLNAME cname,corp.dealer_code  code"
        SQL = SQL & " FROM ittemp.corp_dealers corp,customer_all cus,ccontact_all cc"
        SQL = SQL & " where cc.customer_id=cus.customer_id"
        SQL = SQL & " and cus.customer_id=corp.CORP_DEALER_ID"
        SQL = SQL & " and cc.ccbill='X' and cus.customer_id<1"
        If DS.myORATable(SQL).Rows.Count > 0 Then
            Me.DG_CUST.DataSource = DS.myORATable(SQL)
            Me.DG_CUST.DataBind()
            Me.DG_CUST.Visible = True
        Else
            Me.DG_CUST.Visible = False
            Me.Lblok.Text = "No data found!!"
        End If
        DS = Nothing

    End Sub
End Class
